package edu.dayu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.dayu.enums.DictEnum;
import edu.dayu.mapper.PatientMapper;
import edu.dayu.model.user.Patient;
import edu.dayu.service.DictDubbo;
import edu.dayu.service.PatientService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Autowired
    private PatientMapper patientMapper;

    @DubboReference
    private DictDubbo dictDubbo;

    //获取就诊人列表
    @Override
    public List<Patient> findAllUserId(Long userId) {
        //根据userid查询所有就诊人信息列表
        QueryWrapper<Patient> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        List<Patient> patientList = patientMapper.selectList(wrapper);
        //通过远程调用，得到编码对应具体内容，查询数据字典表内容
        if (patientList.size()>0)
        patientList.stream().forEach(item -> {
            this.packPatient(item);
        });
        return patientList;
    }

    @Override
    public Patient getPatientId(Long id) {
        return this.packPatient(patientMapper.selectById(id));
    }

    //Patient对象里面其他参数封装
//    @SentinelResource(value = "packPatient" ,fallback = "fallback")
    private Patient packPatient(Patient patient) {
        //根据证件类型编码，获取证件类型具体指
        String certificatesTypeString =
                dictDubbo.getNameByParentDictCodeAndValue(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());//联系人证件
        //联系人证件类型
        String contactsCertificatesTypeString =
                dictDubbo.getNameByParentDictCodeAndValue(DictEnum.CERTIFICATES_TYPE.getDictCode(),patient.getContactsCertificatesType());
        //省
        String provinceString = dictDubbo.getNameByParentDictCodeAndValue(null,patient.getProvinceCode());
        //市
        String cityString = dictDubbo.getNameByParentDictCodeAndValue(null,patient.getCityCode());
        //区
        String districtString = dictDubbo.getNameByParentDictCodeAndValue(null,patient.getDistrictCode());
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
        return patient;
    }

    private Patient fallback(){
        return new Patient();
    }
}
